/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-09-02 21:48:23
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-10-23 09:42:34
 * @FilePath: \keep_motion_api\controllers\user\finance.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Finance } = require('@/models/user');
const getFinances = async (req, res) => {
  try {
    const { user_id, user_name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Finance.getFinances(user_id, user_name, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询财务信息失败', error });
  }
}
const updateFinances = async (req, res) => {
  try {
    const { user_id, balanceChange } = req.body;
    const finance = await Finance.updateFinances(user_id, balanceChange);
    res.status(200).send(finance);
  } catch (error) {
    res.status(500).json({ message: '更新财务信息失败', error:error.message });
  }
}
const createCard = async (req, res) => {
  try {
    const { user_id, bank_name, card_number } = req.body;
    const finance = await Finance.createCard(user_id, { bank_name, card_number, binding_date: Date.now() });
    res.status(200).send(finance);
  } catch (error) {
    res.status(500).json({ message: '创建银行卡失败', error });
  }
}
const deleteCard = async (req, res) => {
  try {
    const { user_id, card_id } = req.body;
    const finance = await Finance.deleteCard(user_id, card_id);
    res.status(200).send(finance);
  } catch (error) {
    res.status(500).json({ message: '删除银行卡失败', error });
  }
}

module.exports = {
  getFinances,
  updateFinances,
  createCard,
  deleteCard 
}
